package post

import (
	"context"
	"fmt"
	"strconv"
	"strings"

	"mall/common/globalkey"
	"mall/common/jwt"
	"mall/common/moneyx"
	"mall/common/mqueuex"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/attachment/rpc/attachmentclient"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/forum/rpc/forumclient"
	"mall/service/user/rpc/userclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreatePostLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreatePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePostLogic {
	return &CreatePostLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// 新增香评 ##25.04.01## 历史遗留文件，暂不处理
func (l *CreatePostLogic) CreatePost(req *types.CreatePostReq, ip string) (*types.BaseMsgResp, error) {
	if req.ParentId == nil || *req.ParentId == 0 {
		return nil, resultx.NewErrCode(resultx.FORUM_POST_PARENT_ID_REQUIRED)
	}

	if req.Content == nil || len(strings.TrimSpace(*req.Content)) == 0 {
		return nil, resultx.NewErrCode(resultx.FORUM_POST_CONTENT_REQUIRED)
	}

	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}

	userInfo, err := l.svcCtx.UserRpc.GetUserAuthByUserId(l.ctx, &userclient.GetUserAuthByUserIdReq{
		PlatformId: pointy.GetPointer(globalkey.ThirdPartyPlatformWechatMini),
		UserId:     pointy.GetPointer(userId),
		WithRank:   pointy.GetPointer(uint32(1)),
	})
	if err != nil {
		logx.Infow("CreatePost UserRpc.GetUserAuthByUserId err", logx.Field("detail", err.Error()))
		return nil, resultx.NewErrCode(resultx.USER_GET_INFO_FAILED)
	}

	if userInfo.Openid == nil {
		return nil, resultx.NewErrCode(resultx.USER_GET_INFO_FAILED)
	}

	author := &forumclient.ForumAuthorInfo{
		Id:     userInfo.UserId,
		Name:   userInfo.Nickname,
		Avatar: userInfo.Avatar,
		Ip:     pointy.GetPointer(ip),
		Level:  userInfo.RankId,
	}

	newThumbMap := make(map[string]*types.MediaInfo)
	var newMedia []*forumclient.ForumMediaInfo
	var newMediaIds []string
	var newAttachmentIds []string
	if len(req.Photos) > 0 {
		for idx, v := range req.Photos {
			if v.Order == nil {
				v.Order = pointy.GetPointer(uint32(idx))
			}
			if v.Id != nil && len(*v.Id) > 0 {
				if _, ok := newThumbMap[*v.Id]; !ok { // 忽略重复id
					newThumbMap[*v.Id] = v
					newMediaIds = append(newMediaIds, *v.Id)
				}
			} else {
				var thumbnail *uint32
				if v.Thumbnail != nil && *v.Thumbnail {
					thumbnail = pointy.GetPointer(uint32(1))
				}
				newMedia = append(newMedia, &forumclient.ForumMediaInfo{ // 第三方图片
					Uid:       v.Uid,
					Url:       v.Url,
					Thumbnail: thumbnail,
					AltText:   v.AltText,
					Meta:      v.Details,
					Order:     v.Order,
				})
			}
		}
	}

	// 根据 newMediaIds 查询 attachments 记录到 forum_attachment_relationships
	if len(newMediaIds) > 0 {
		res, err := l.svcCtx.AttachmentRpc.BackendGetFileList(l.ctx, &attachmentclient.FileListReq{
			Ids: pointy.GetPointer(strings.Join(newMediaIds, ",")),
		})
		if err != nil {
			return nil, err
		}

		if len(res.List) > 0 {
			for _, v := range res.List {
				var url *string
				if v.Url != nil && len(*v.Url) > 0 {
					url = pointy.GetPointer(fmt.Sprintf("%s%s", l.svcCtx.Config.NfsConf.StaticUrl, *v.Url))
				}
				if url == nil && v.Path != nil && len(*v.Path) > 0 {
					url = pointy.GetPointer(fmt.Sprintf("%s%s", l.svcCtx.Config.NfsConf.ProxyUrl, *v.Path))
				}
				if media, ok := newThumbMap[*v.Id]; ok {
					var thumbnail *uint32
					if media.Thumbnail != nil && *media.Thumbnail {
						thumbnail = pointy.GetPointer(uint32(1))
					}
					newMedia = append(newMedia, &forumclient.ForumMediaInfo{
						Uid:       media.Uid,
						Id:        v.Id,
						Uri:       media.Url,
						Url:       url,
						Thumbnail: thumbnail,
						AltText:   media.AltText,
						Meta:      media.Details,
						Order:     media.Order,
					})
					newAttachmentIds = append(newAttachmentIds, *v.Id)
				}
			}
		}
	}

	var forumId uint64
	if req.ForumId != nil && *req.ForumId > 0 {
		forumId = *req.ForumId
	} else {
		forumId = uint64(l.svcCtx.Config.Query.PostForumId)
	}
	var commentStatus *uint32
	if req.CommentStatus != nil && *req.CommentStatus {
		commentStatus = pointy.GetPointer(uint32(1))
	}

	var meta []*forumclient.ForumMetaInfo
	if len(req.Meta) > 0 {
		for _, v := range req.Meta {
			if v.Key == nil || v.Value == nil {
				continue
			}

			if len(strings.TrimSpace(*v.Key)) == 0 || len(strings.TrimSpace(*v.Value)) == 0 {
				continue
			}

			meta = append(meta, &forumclient.ForumMetaInfo{
				Id:    v.Id,
				Key:   v.Key,
				Value: v.Value,
			})
		}
	}

	if len(req.Notes) > 0 {
		noteMap := make(map[string]int)
		for idx, v := range req.Notes {
			if v.Key == nil || v.Value == nil {
				continue
			}

			if len(strings.TrimSpace(*v.Key)) == 0 || len(strings.TrimSpace(*v.Value)) == 0 {
				continue
			}

			if _, ok := noteMap[*v.Key]; !ok {
				noteMap[*v.Key] = idx
				meta = append(meta, &forumclient.ForumMetaInfo{
					Id:    v.Id,
					Key:   v.Key,
					Value: v.Value,
				})
				meta = append(meta, &forumclient.ForumMetaInfo{ // 附加的备注行：气味
					Key:   pointy.GetPointer("_note"),
					Value: v.Key,
				})
			}
		}
	}
	var averageRating *string
	if len(req.Grades) > 0 {
		gradeMap := make(map[string]int)
		var total int64
		var count int64
		for idx, v := range req.Grades {
			if v.Key == nil || v.Value == nil {
				continue
			}

			if len(strings.TrimSpace(*v.Key)) == 0 || len(strings.TrimSpace(*v.Value)) == 0 {
				continue
			}

			if _, ok := gradeMap[*v.Key]; !ok {
				if i, err := strconv.ParseInt(*v.Value, 10, 32); err == nil {
					gradeMap[*v.Key] = idx
					total += i
					count += 1
					meta = append(meta, &forumclient.ForumMetaInfo{
						Id:    v.Id,
						Key:   v.Key,
						Value: v.Value,
					})
					meta = append(meta, &forumclient.ForumMetaInfo{ // 附加的备注行：评分
						Key:   pointy.GetPointer("_grade"),
						Value: v.Key,
					})
				}
			}
		}
		if count > 0 {
			averageRating = pointy.GetPointer(moneyx.FormatStar((float64(total) / float64(count)), 1))
		}
	}

	res, err := l.svcCtx.ForumRpc.CreatePost(l.ctx, &forumclient.PostInfo{
		ForumId:       pointy.GetPointer(forumId),
		ParentId:      req.ParentId,
		ContentHtml:   pointy.GetPointer(strings.TrimSpace(*req.Content)),
		CommentStatus: commentStatus,
		AverageRating: averageRating,
		Author:        author,
		NewMedia:      newMedia,
		Meta:          meta,
	})
	if err != nil {
		return nil, err
	}

	if len(newAttachmentIds) > 0 {
		_, err = l.svcCtx.AttachmentRpc.BackendCreateLog(l.ctx, &attachmentclient.CreateLogReq{
			FileIds:    newAttachmentIds,
			UserId:     pointy.GetPointer(strconv.FormatInt(int64(userId), 10)),
			Put:        pointy.GetPointer(uint32(1)),
			Thumbnail:  pointy.GetPointer(uint32(1)),
			Watermark:  pointy.GetPointer(uint32(1)),
			TermId:     pointy.GetPointer(forumId),
			ObjectId:   pointy.GetPointer(res.Id),
			ObjectType: pointy.GetPointer(uint32(globalkey.AttachmentLogTypeForumPost)),
			Title:      pointy.GetPointer("review"),
			Slug:       pointy.GetPointer(strconv.FormatInt(int64(*req.ParentId), 10)),
		})
		if err != nil {
			logx.Errorw("CreatePost AttachmentRpc BackendCreateLog err", logx.Field("detail", err.Error()))
		}
	}

	var userMeta []*userclient.UserMetaInfo
	userMeta = append(userMeta, &userclient.UserMetaInfo{
		Key:   pointy.GetPointer(mqueuex.UserStatisticForumReviewNew), // "_review_new_status"
		Value: pointy.GetPointer("-1"),
	})
	l.svcCtx.UserRpc.SetUserMeta(l.ctx, &userclient.SetUserMetaReq{
		UserId:   pointy.GetPointer(userId),
		MetaKey:  pointy.GetPointer(mqueuex.UserStatisticForumReviewCount), // "_review_count"
		Incr:     pointy.GetPointer(int32(1)),
		UserMeta: userMeta,
	})

	return &types.BaseMsgResp{
		Msg: "发布成功",
	}, nil
}
